<template>
  <div class="selectMenubxo">
    <van-sticky>
      <div class="search-module">
        <van-search show-action v-model="search" @search="onSearch" @cancel="onCancel"></van-search>
      </div>
    </van-sticky>
    <div>
      <van-list
        v-if="listAll.length>0"
        v-model="loading"
        finished-text="No more"
        :finished="finished"
        @load="onLoad"
        class="news-box"
      >
        <van-cell
          class="news-item"
          v-for="(i,index) in list"
          :key="index"
          :class="{hotnews:i.top==1}"
        >
          <newsItem :i="i"></newsItem>
        </van-cell>
      </van-list>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
import dings from "../../assets/img/icon-ding.png";
import { searchinfolist } from "../../api/sort";
import newsItem from "../../components/newslist/newstLi";
export default {
  components: {
    newsItem
  },
  data() {
    return {
      search: "",
      dings,
      list: [],
      finished: false,
      listAll: [], //所有新闻，规定只给50条，当所有新闻,自己做分页功能
      error: false,
      loading: false,
      channel: this.$route.query.channel, //代表菜单种类
      page: 0,
      pageSize: 10
    };
  },
  methods: {
    newsDetail(i) {
      this.$router.push({ name: "newsDetails", query: i });
    },
    onLoad() {
      // 异步更新数据
      setTimeout(() => {
         let begin = this.page * this.pageSize;
        let end = (this.page + 1) * this.pageSize;
        for (let i = begin; i < end; i++) {
          if (this.listAll[i]) {
             this.list.push(this.listAll[i]);
          }
        }
        this.page++;
        // 加载状态结束
        this.loading = false;
        // 数据全部加载完成
        if (this.list.length >= this.listAll.length) {
          this.finished = true;
        }
      }, 500);
    },
    //获取新闻信息
    searchinfolist() {
      if (!this.search) {
        return;
      }
      this.innitArr();
      let obj = {};
      obj.channel = this.$route.query.channel || "";
      obj.kw = this.search;
      searchinfolist(obj).then(res => {
        if (res.code === "0000") {
          this.listAll = res.data || [];
          if (res.data.length == 0) {
            this.$toast("没有相关数据");
          }
        }
      });
    },

    onSearch() {
      this.searchinfolist();
    },
    onCancel() {
      this.innitArr();
      this.$router.go(-1);
    },
    innitArr() {
      this.list = [];
      this.listAll = [];
      this.finished = false;
      this.finishedText = "";
      this.error = false;
      this.loading = false;
      this.page = 0;
      this.pageSize = 10;
    }
  }
};
</script>

<style scoped lang="less">
.selectMenubxo {
  position: relative;
  width: 100%;
  min-height: 100%;
  top: 0;
  left: 0;
  background: #ffffff;
  z-index: 111;
}
</style>
